package com.niiwoo.civet.mobile.controller.kaola;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.civet.mobile.vo.request.kaola.KaoLaLiCaiFullRepayRequestVO;
import com.niiwoo.civet.mobile.vo.request.kaola.KaoLaLiCaiImmediateRepayRequestVO;
import com.niiwoo.civet.mobile.vo.request.kaola.KaoLaLiCaiRepayRequestVO;
import com.niiwoo.civet.mobile.vo.response.koala.KaoLaLiCaiFullRepayReponseVO;
import com.niiwoo.civet.mobile.vo.response.koala.KaoLaLiCaiImmediateRepayResponseVO;
import com.niiwoo.civet.mobile.vo.response.koala.KaoLaLiCaiRepayResponseVO;
import com.niiwoo.civet.trade.dto.request.kaola.KaoLaLiCaiRepayRequestDTO;
import com.niiwoo.civet.trade.dto.request.myloan.MyProjectRequestDTO;
import com.niiwoo.civet.trade.service.koala.KaoLaLiCaiDubboService;
import com.niiwoo.tripod.web.util.PrincipalContext;
import com.niiwoo.tripod.web.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Optional;

import static com.niiwoo.tripod.web.vo.Result.with;

/**
 * 考拉理财app controller
 *
 * @author lichuanzhi
 * @date 2018/01/11
 */

@Slf4j
@RestController
@RequestMapping("/kaolalicai")
@Api(tags = "考拉理财（李传志）", description = "/kaolalicai")
public class KaoLaLiCaiController {

    @Reference(version = "1.0.0")
    private KaoLaLiCaiDubboService kaoLaLiCaiRepayDubboService;

    @Reference(version = "1.0.0")
    private BankCardDubboService bankCardDubboService;

    @PostMapping("/repay")
    @ApiOperation(value = "考拉理财还款", notes = "TRD10001-订单不存在；TRD20001-标的不存在；TRD20005-回款计划不存在；TRD20006-还款失败；TRD21001-您的还款请求正在处理中；" +
            "TRD21002-您已还本期，请次日再操作全额还款，谢谢。；")
    public Result<KaoLaLiCaiRepayResponseVO> repay(@RequestBody @Valid KaoLaLiCaiRepayRequestVO requestVO) {
        KaoLaLiCaiRepayRequestDTO requestDTO = new KaoLaLiCaiRepayRequestDTO();
        BeanUtils.copyProperties(requestVO,requestDTO);
        requestDTO.setUserId(PrincipalContext.getUserId());
        KaoLaLiCaiRepayResponseVO responseVO = new KaoLaLiCaiRepayResponseVO();
        BeanUtils.copyProperties(kaoLaLiCaiRepayDubboService.repayment(requestDTO),responseVO);
        return with(responseVO);
    }

    @PostMapping("/repay/immediate")
    @ApiOperation("立即还款弹窗")
    public Result<KaoLaLiCaiImmediateRepayResponseVO> immediateRepay(@RequestBody @Valid KaoLaLiCaiImmediateRepayRequestVO requestVO) {
        MyProjectRequestDTO requestDTO = new MyProjectRequestDTO();
        requestDTO.setProjectId(requestVO.getProjectId());
        KaoLaLiCaiImmediateRepayResponseVO responseVO = JSON.parseObject(JSON.toJSONString(kaoLaLiCaiRepayDubboService
                .immediateRepay(requestDTO)), KaoLaLiCaiImmediateRepayResponseVO.class);
        responseVO.setBankCard(this.getIdCard(PrincipalContext.getUserId()));
        return with(responseVO);
    }

    @PostMapping("/repay/full")
    @ApiOperation("提前结清弹窗")
    public Result<KaoLaLiCaiFullRepayReponseVO> fullRepay(@RequestBody @Valid KaoLaLiCaiFullRepayRequestVO requestVO) {
        MyProjectRequestDTO requestDTO = new MyProjectRequestDTO();
        requestDTO.setProjectId(requestVO.getProjectId());
        KaoLaLiCaiFullRepayReponseVO responseVO = new KaoLaLiCaiFullRepayReponseVO();
        BeanUtils.copyProperties(kaoLaLiCaiRepayDubboService.fullRepay(requestDTO),responseVO);
        responseVO.setBankCard(this.getIdCard(PrincipalContext.getUserId()));
        return with(responseVO);
    }

    private String getIdCard(String userId) {
        return Optional.ofNullable(bankCardDubboService.getUserBankCardInfo(userId)).map(r -> StringUtils.substring(r.getBankAccountNo(),-4)).orElse(null);
    }
}
